package com.superman.view;

import com.superman.entity.Car_Brand_Category;
import com.superman.entity.User;
import com.superman.util.Constants;
import com.superman.util.JPanelCarAdmin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

public class AlterUI {

    private User user;
    Query query = new Query();
    AddAndAlter addAndAlter = new AddAndAlter();
    JPanelCarAdmin jPanelCar = new JPanelCarAdmin();

    private JFrame frame = new JFrame("管理员修改汽车信息");
    private Container container = frame.getContentPane();

    //上
    private JPanel upside_panel = new JPanel();
    private JLabel car_lab = new JLabel("汽车们：");
    private JComboBox car_cb;
    private JLabel choose_lab = new JLabel("修改内容：");
    private JComboBox choose_cb = new JComboBox(Constants.choose);

    //中
    private JPanel middle_panel = new JPanel();
    private JPanel alter_panel;

    //下
    private JPanel downside_panel = new JPanel();
    private JLabel change_lab = new JLabel("修改后的值：");
    private JTextField change_tf = new JTextField(20);
    private JButton alter_btn = new JButton("确认修改");
    private JButton backup_btn = new JButton("返回");
    private JButton quit_btn = new JButton("退出");
    private JLabel result_lab = new JLabel("二嗨租车修改工作进行中...");

    public AlterUI(User user){
        frame.setSize(1000,500);
        container.setLayout(new BorderLayout());
        initFrame(user);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void initFrame(User user){
        //上侧
        upside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 20, 20));
        List<Car_Brand_Category> list = query.adminQueryCar();
        Vector<String> vector = new Vector<>();
        for (Car_Brand_Category cbc:list){
            String s = cbc.getCar_number() + "-" + cbc.getModel() + "-" + cbc.getColor() + "-" +
                    cbc.getT_comments() + "-" + cbc.getBrand() + "-" + cbc.getCategory() + "-（租赁价格）" +
                    cbc.getRent() + "-" + ((cbc.getUseable() == 0)? "上架":"下架");
            vector.add(s);
        }
        car_cb = new JComboBox(vector);
        upside_panel.add(car_lab);
        upside_panel.add(car_cb);
        upside_panel.add(choose_lab);
        upside_panel.add(choose_cb);
        container.add(upside_panel, "North");

        //中部
        middle_panel.setLayout(new FlowLayout(20,20,20));
        container.add(middle_panel, "Center");

        //下侧
        downside_panel.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 30));
        downside_panel.add(change_lab);
        downside_panel.add(change_tf);
        downside_panel.add(alter_btn);
        downside_panel.add(backup_btn);
        downside_panel.add(quit_btn);
        downside_panel.add(result_lab);
        container.add(downside_panel, "South");

        alter_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String car = String.valueOf(car_cb.getSelectedItem());
                String car_number = car.split("-")[0];
                String car_id = null;
                for (Car_Brand_Category cbc:list){
                    if (cbc.getCar_number().equals(car_number)){
                        car_id = cbc.getId();
                    }
                }
                String choose = String.valueOf(choose_cb.getSelectedItem());
                String change = change_tf.getText();
                switch (choose){
                    case "租赁价格":
                        List<Car_Brand_Category> list_r = addAndAlter.alterCarRent(car_id, change);
                        if (list_r.isEmpty()){
                            result_lab.setText("修改失败！自己好好找找原因");
                        }else {
                            result_lab.setText("修改成功！");
                            alter_panel = jPanelCar.getJPanelCar(list_r);
                            middle_panel.add(alter_panel);
                        }
                        break;
                    case "上架下架":
                        int useable = change.equals("上架")? 0:1;
                        List<Car_Brand_Category> list_u = addAndAlter.alterCarUseable(car_id, useable);
                        if (list_u.isEmpty()){
                            result_lab.setText("修改失败！自己找找原因");
                        }else {
                            result_lab.setText("修改成功！");
                            alter_panel = jPanelCar.getJPanelCar(list_u);
                            middle_panel.add(alter_panel);
                        }
                        break;
                }
            }
        });

        backup_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new AdminUI(user);
            }
        });

        quit_btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                hide();
                new LoginUI();
            }
        });
    }
    public void hide(){
        frame.setVisible(false);
    }
}
